<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <el-form ref="form" :model="formList" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formList.name" placeholder="2-10位字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formList.code" placeholder="2-10位字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formList.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option v-for="(item) in mangerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formList.introduce" type="textarea" placeholder="1-100个字符" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="confirm">确定</el-button>
            <el-button size="mini">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { addDepartment, getDepartments, getManagers, getDepartmentDetail, updateDepartment } from '@/api/department'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formList: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 查询所有的部门列表数据
              // 判断输入的部门名称是否已经存在列表中
              // 如果存在，提示部门名称已经存在，不通过校验
              // 如果不存在，通过校验
              let res = await getDepartments()
              if (this.formList.id) {
                // 排除自身，过滤
                res = res.filter(item => item.id !== this.formList.id)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('部门名称已经存在'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getDepartments()
              // 排除自身，过滤
              if (this.formList.id) {
                res = res.filter(item => item.id !== this.formList.id)
              }
              if (res.some(item => item.code === value)) {
                callback(new Error('部门编码已经存在'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      },
      mangerList: []
    }
  },
  computed: {
    title() {
      return this.formList.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async confirm() {
      await this.$refs.form.validate() // 校验表单
      if (this.formList.id) {
        await updateDepartment(this.formList)
        this.$message.success('修改部门成功')
      } else {
        await addDepartment({ ...this.formList, pid: this.currentNodeId })
        this.$message.success('添加部门成功')
      }
      this.$emit('updateDepartment')
      this.close()
      console.log('校验通过')
    },
    close() {
      this.formList = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.form.resetFields()// 重置表单
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      const res = await getManagers()
      this.mangerList = res
    },
    async getDepartDetails() {
      console.log('发请求')
      // 发请求
      this.formList = await getDepartmentDetail(this.currentNodeId)
    }
  }
}
</script>
